package guinew.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.xml.parsers.ParserConfigurationException;

import app.XMLGenerator;

public class GenerationView extends JFrame {

	private static final long serialVersionUID = -4040021203239216186L;

	private JTextField depthTextField;
	private JTextField widthTextField;
	private JCheckBox fullCheckBox;
	private JCheckBox binaryCheckBox;
	private JPanel mainPanel;
	
	public GenerationView(){
		super();
		initGUI();
	}
	
	private void initGUI(){	
		this.setTitle("Generate XML File");
		this.getContentPane().setLayout(new BorderLayout());
		
		mainPanel = new JPanel();
		mainPanel.setLayout(new GridLayout(0,2));
		this.getContentPane().add(mainPanel, BorderLayout.CENTER);
		
		JLabel depthLabel = new JLabel("Depth:");
		mainPanel.add(depthLabel);
		depthTextField = new JTextField();
		mainPanel.add(depthTextField);
		
		JLabel widthLabel = new JLabel("Width:");
		mainPanel.add(widthLabel);
		widthTextField = new JTextField();
		mainPanel.add(widthTextField);
		
		JLabel fullLabel = new JLabel("Full Tree:");
		mainPanel.add(fullLabel);
		fullCheckBox = new JCheckBox();
		mainPanel.add(fullCheckBox);
		
		JLabel binaryLabel = new JLabel("Binary Tree:");
		mainPanel.add(binaryLabel);
		binaryCheckBox = new JCheckBox();
		mainPanel.add(binaryCheckBox);
		

		
		JButton submit = new JButton("Generate");
		submit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				final JFileChooser fileChooser = new JFileChooser();
		
				fileChooser.setDialogTitle("Save as...");
				fileChooser.setApproveButtonText("Save");

				int returnVal = fileChooser.showOpenDialog(mainPanel);
				if(returnVal == JFileChooser.APPROVE_OPTION){
					try {
						XMLGenerator xmlGen = new XMLGenerator(fileChooser.getSelectedFile());
						xmlGen.generate(Long.parseLong(depthTextField.getText()), Long.parseLong(widthTextField.getText()), fullCheckBox.isSelected(), binaryCheckBox.isSelected());
					} catch (ParserConfigurationException e) {
						e.printStackTrace();
					}
									
				}	
			}		
		});
		
		mainPanel.add(submit);
		
		pack();
		this.setSize(300, 250);
		this.setLocationRelativeTo(null);
		this.setVisible(true);  
	}
}
